package com.aqiang.day0528_shop.main.adapter

import android.content.Context
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.aqiang.day0528_shop.R
import com.aqiang.day0528_shop.main.entity.GoodsTypeEntity
import android.view.LayoutInflater
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.zy.shopproject2.usercenter.entity.BaseResponseEntity


class NewAdapter(var list: List<GoodsTypeEntity>,var con:Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val layoutInflater: LayoutInflater
    init {
        layoutInflater = LayoutInflater.from(con)
    }
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
        if(p1 == 0){
            return TopHolder(layoutInflater.inflate(R.layout.item_second_top,null))
        }else{
            return ButtomHolde(layoutInflater.inflate(R.layout.item_second_buttom,null))
        }
    }

    override fun getItemViewType(position: Int): Int {
        return position % 2
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
        if (getItemViewType(p1) == 0){
            val p01 = p0 as TopHolder
            p01.mTitle!!.text = list.get(p1).category_name
        }else{
            val p01 = p0 as ButtomHolde
            var recyclerView = p01.recyclerView
            recyclerView!!.layoutManager = GridLayoutManager(con,3)
            OkGo.get<String>("http://www.city2sky.cn/api/GoodsType/getTypes?")
                .params("parentId",list.get(p1).category_id)
                .headers("Authorization"," bearer alK2jh4j0D9992g_uK-8UvXXpXJ3bJ21aWlVkhg9OIqlbfAbYqnUrPD1iS5on8RsfqgfsyyE8AUzJEaSaTox3TkXCP_Yr7ERGdJUVhISiNenGwBhZn4nVojWJvWToARMFURQ5RB72-2Zx7U9OvL7XJVqZFb78RLJdkxI6iIPyfxsBhVrZS2nOCsYWtuIj-8qWdfXPSkHJCwGN_LkPJDzsEKVnbuTglU9AvMSBKg8CvbRwZG0rft84xOLPUmEQm-SGgK7pLSlbdJ8cz7m5YdSGpVYiejzQJjJcBhExqV5uRI")
                .execute(object : StringCallback(){
                    override fun onSuccess(response: Response<String>?) {
                        val entity = Gson().fromJson(response?.body(), BaseResponseEntity::class.java)
                        //val list = entity.data as List<GoodsTypeEntity>
                        //val data = entity.data as List<GoodsTypeEntity>
                        val list = entity.getValue<List<GoodsTypeEntity>>()
                        //SLog.d("${data}")
                        if (list == null){

                        }else{
                            //recyclerView!!.adapter = TAdapter(R.layout.item_t,list!!)
                        }

//                        if(tAdapter == null){
//                            tAdapter == TAdapter(R.layout.item_t,data)
//                            rv.adapter = tAdapter
//                            SLog.d("11")
//                        }else{
//                            SLog.d("22")
//                            tAdapter?.data!!.clear()
//                            tAdapter?.data!!.addAll(data)
//                            tAdapter!!.notifyDataSetChanged()
//                        }
                    }

                })
        }
    }

    class TopHolder(itemView:View) : RecyclerView.ViewHolder(itemView){
        var mTitle: TextView? = null
        init {
            mTitle = itemView.findViewById(com.aqiang.day0528_shop.R.id.tv_item_second_title_top)
        }
    }

    class ButtomHolde(itemView:View) : RecyclerView.ViewHolder(itemView){
        var recyclerView:RecyclerView? = null
        init {
            recyclerView = itemView.findViewById(com.aqiang.day0528_shop.R.id.rv_item_second_buttom)
        }
    }
}